home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 15 / Example 15.2 / lobby.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-17  |  9.9 KB  |  349 lines

  1. #include "lobby.h"
  2. #include "app.h"
  3.  
  4. extern APPLICATION app;
  5.  
  6. LOBBY::LOBBY()
  7. {
  8.     room = 0;
  9.     m_pBackGround = m_pUI = NULL;
  10.     m_pMiniText = m_pSmallText = m_pCaption = NULL;
  11.     m_pDevice = NULL;
  12.     m_pSprite = NULL;
  13. }
  14.  
  15. LOBBY::~LOBBY()
  16. {
  17.     //Release all resources
  18.     if(m_pBackGround)m_pBackGround->Release();
  19.     if(m_pUI)m_pUI->Release();
  20.     if(m_pMiniText)m_pMiniText->Release();
  21.     if(m_pSmallText)m_pSmallText->Release();
  22.     if(m_pCaption)m_pCaption->Release();
  23.     if(m_pSprite)m_pSprite->Release();
  24. }
  25.  
  26. void LOBBY::Init(IDirect3DDevice9* Dev)
  27. {
  28.     room = 0;
  29.     m_pDevice = Dev;
  30.  
  31.     //Load textures
  32.     D3DXCreateTextureFromFile(m_pDevice, "textures/background.jpg", &m_pBackGround);
  33.     D3DXCreateTextureFromFile(m_pDevice, "textures/UI.dds", &m_pUI);
  34.  
  35.     //Create fonts
  36.     D3DXCreateFont(m_pDevice, 14, 0, 0, 1, false,  
  37.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  38.                    DEFAULT_PITCH | FF_DONTCARE, "Courier New", &m_pMiniText);    
  39.  
  40.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  41.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  42.                    DEFAULT_PITCH | FF_DONTCARE, "Courier New", &m_pSmallText);    
  43.  
  44.     D3DXCreateFont(m_pDevice, 24, 0, FW_BOLD, 1, false,  
  45.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  46.                    DEFAULT_PITCH | FF_DONTCARE, "Courier New", &m_pCaption);    
  47.  
  48.     //Create sprite and line interface
  49.     D3DXCreateSprite(m_pDevice, &m_pSprite);
  50.     D3DXCreateLine(m_pDevice, &m_pLine);
  51.     m_pLine->SetAntialias(true);
  52.  
  53.     //Init Lobby Content
  54.     RECT nRect[] = {{200, 300, 600, 330}, {30, 540, 640, 570}};
  55.     m_nameTB.Init(nRect[0], 12);
  56.     m_sessionTB.Init(nRect[0], 32);
  57.     m_sendTB.Init(nRect[1], 50);
  58. }
  59.  
  60. void LOBBY::Update(float deltaTime, MOUSE &mouse)
  61. {
  62.     //Update textboxes
  63.     if(room == 0)
  64.         m_nameTB.Update(deltaTime, mouse);
  65.     else if(room == 2)
  66.         m_sessionTB.Update(deltaTime, mouse);
  67.     else if(room == 4)
  68.         m_sendTB.Update(deltaTime, mouse);
  69. }
  70.  
  71. void LOBBY::Draw(MOUSE &mouse)
  72. {
  73.     //Draw m_pBackGround
  74.     D3DXMATRIX sca;
  75.     D3DXMatrixScaling(&sca, 3.125f, 2.35f, 1.0f);
  76.  
  77.     m_pSprite->SetTransform(&sca);
  78.     m_pSprite->Begin(0);
  79.     m_pSprite->Draw(m_pBackGround, NULL, NULL, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), 0xffffffff);
  80.     m_pSprite->End();
  81.     
  82.     D3DXMatrixIdentity(&sca);
  83.     m_pSprite->SetTransform(&sca);
  84.  
  85.     switch(room)
  86.     {
  87.         case 0:    //Create Player room
  88.         {
  89.             RECT rc = {0, 270, 800, 300};
  90.             m_pCaption->DrawText(NULL, "Enter Your Name:", -1, &rc, DT_CENTER | DT_TOP | DT_NOCLIP, 0xffffffff);
  91.             m_nameTB.active = true;
  92.             DrawTextbox(m_nameTB);
  93.  
  94.             RECT nextR = {520, 360, 600, 390};
  95.             if(m_nameTB.text.size() > 3 && (TextButton("Next", nextR, true, mouse) || KEYDOWN(VK_RETURN)))
  96.             {
  97.                 m_sessionTB.text = m_nameTB.text + "'s Game";
  98.                 room = 1;
  99.             }
  100.             break;
  101.         }
  102.         case 1:    //Host or Join room. (server/client)
  103.         {
  104.             RECT r[] = {{250, 250, 550, 280}, {250, 320, 550, 350}};
  105.             if(TextButton("Host Game", r[0], true, mouse))
  106.             {
  107.                 network.Init(true, (char*)m_nameTB.text.c_str());
  108.                 room = 2;
  109.             }
  110.  
  111.             if(TextButton("Join Game", r[1], true, mouse))
  112.             {
  113.                 network.Init(false, (char*)m_nameTB.text.c_str());
  114.                 network.FindSessions();
  115.                 room = 3;
  116.             }
  117.  
  118.             break;
  119.         }
  120.         case 2:    //Create Session room
  121.         {
  122.             RECT rc = {0, 270, 800, 300};
  123.             m_pCaption->DrawText(NULL, "Enter Session Name:", -1, &rc, DT_CENTER | DT_TOP | DT_NOCLIP, 0xffffffff);
  124.             m_sessionTB.active = true;
  125.             DrawTextbox(m_sessionTB);
  126.  
  127.             RECT nextR = {520, 360, 600, 390};
  128.             if(m_sessionTB.text.size() > 3 && (TextButton("Next", nextR, true, mouse) || KEYDOWN(VK_RETURN)))
  129.             {
  130.                 network.HostNewSession((char*)m_sessionTB.text.c_str());
  131.                 room = 4;
  132.             }
  133.             break;
  134.         }
  135.         case 3:    //Join Session room
  136.         {
  137.             RECT r[] = {{50, 50, 750, 550}, {50, 50, 750, 100}, {600, 60, 740, 90}};
  138.  
  139.             Square(r[0], 0xffffffff);
  140.             Square(r[1], 0xffffffff);
  141.  
  142.             RECT rc = {70, 60, 0, 0};
  143.             m_pCaption->DrawText(NULL, "Active Games", -1, &rc, DT_LEFT | DT_TOP | DT_NOCLIP, 0xffffffff);
  144.             if(TextButton("Refresh", r[2], true, mouse))network.FindSessions();
  145.  
  146.             //Draw list of available sessions
  147.             for(int i=0;i<network.sessions.size();i++)
  148.             {
  149.                 RECT rs = {60, 110 + 40 * i, 600, 140 + 40 * i};
  150.                 char number[10];
  151.                 std::string text = _itoa(i + 1, number, 10);
  152.                 text += ": ";
  153.                 text += network.sessions[i].name;
  154.  
  155.                 if(TextButton((char*)text.c_str(), rs, false, mouse))
  156.                 {
  157.                     HRESULT hr = network.ConnectToSession(i);
  158.  
  159.                     //Connection failed...
  160.                     if(FAILED(hr))
  161.                     {
  162.                         if(hr == DPNERR_HOSTREJECTEDCONNECTION)
  163.                             m_error = "Host rejected connection";
  164.                         else if(hr == DPNERR_NOCONNECTION)
  165.                             m_error = "No connection was made";
  166.                         else if(hr == DPNERR_NOTHOST)
  167.                             m_error = "Host not found";
  168.                         else if(hr == DPNERR_SESSIONFULL)
  169.                             m_error = "Session is full";
  170.                         else if(hr == DPNERR_ALREADYCONNECTED)
  171.                             m_error = "You are already connected";
  172.                         else if(hr == DPNERR_INVALIDAPPLICATION)
  173.                             m_error = "Invalid GUID";
  174.                         else if(hr == DPNERR_INVALIDDEVICEADDRESS)
  175.                             m_error = "Invalid m_pDevice Address";
  176.                         else if(hr == DPNERR_INVALIDFLAGS)
  177.                             m_error = "Invalid Flags";
  178.                         else if(hr == DPNERR_INVALIDHOSTADDRESS)
  179.                             m_error = "Invalid Host Address";
  180.                         else if(hr == DPNERR_INVALIDINSTANCE)
  181.                             m_error = "Invalid GUID 2";
  182.                         else if(hr == DPNERR_INVALIDINTERFACE)
  183.                             m_error = "Invalid Interface";
  184.                         else if(hr == DPNERR_INVALIDPASSWORD)
  185.                             m_error = "Invalid Password";
  186.                         else m_error = "Unknown Error";
  187.  
  188.                         room = 999;
  189.                     }
  190.                     else room = 4;
  191.                 }
  192.             }
  193.  
  194.             break;
  195.         }
  196.         case 4:    //Chat room
  197.         {
  198.             RECT r[] = {{20, 20, 500, 470}, {20, 20, 500, 70}, {520, 20, 780, 470}, {520, 20, 780, 70}, {20, 490, 780, 580}};
  199.             RECT btn[] = {{660, 540, 770, 570}};
  200.  
  201.             for(int i=0;i<5;i++)
  202.                 Square(r[i], 0xffffffff);
  203.  
  204.             m_sendTB.active = true;
  205.  
  206.             RECT r1[] = {{40, 30, 0, 0}, {540, 30, 0, 0}, {40, 500, 0, 0}};
  207.             m_pCaption->DrawText(NULL, network.activeSession, -1, &r1[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  208.             m_pCaption->DrawText(NULL, "Players", -1, &r1[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  209.             m_pCaption->DrawText(NULL, "Send Text:", -1, &r1[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  210.  
  211.             DrawTextbox(m_sendTB);
  212.  
  213.             //Send text message
  214.             if(TextButton("Send", btn[0], true, mouse) || KEYDOWN(VK_RETURN))
  215.                 if(m_sendTB.text.size() > 0)
  216.                 {
  217.                     std::string c = network.playerName;
  218.                     c += " says: ";
  219.                     c += m_sendTB.text;
  220.                     if(network.server)network.chat.push_back(c);
  221.                     if(network.chat.size() > 20)network.chat.erase(network.chat.begin());
  222.  
  223.                     MSG_TEXT msg((char*)c.c_str());
  224.                     network.Send(&msg, true);
  225.                     m_sendTB.text.clear();
  226.                 }
  227.  
  228.             //Draw player list
  229.             for(int i=0;i<network.players.size();i++)
  230.             {
  231.                 RECT rc = {530, 90 + 40 * i, 0, 0};
  232.                 m_pCaption->DrawText(NULL, network.players[i].name, -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  233.             }
  234.  
  235.             //Draw chat
  236.             for(int i=0;i<network.chat.size();i++)
  237.             {
  238.                 RECT rc = {30, 80 + 19 * (network.chat.size() - i - 1), 0, 0};
  239.                 m_pSmallText->DrawText(NULL, network.chat[i].c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  240.             }
  241.  
  242.             //Check if the connection was terminated
  243.             if(!network.connected)
  244.             {
  245.                 m_error = "Session Terminated";
  246.                 room = 999;
  247.             }
  248.             else if(network.server && network.players.size() > 1)
  249.             {
  250.                 RECT rs = {530, 430, 770, 460};
  251.                 if(TextButton("Start Game", rs, true, mouse))
  252.                 {
  253.                     //Send terrain to all clients before staring game...
  254.                     MSG_TERRAIN terr(app.m_terrain, 0);
  255.                     network.Send(&terr, true);
  256.                     room = 5;
  257.                 }
  258.             }
  259.  
  260.             break;
  261.         }
  262.         case 5:        //Transfer initial info Room
  263.         {
  264.             for(int i=0;i<network.players.size();i++)
  265.             {
  266.                 std::string play = network.players[i].name;
  267.                 if(network.players[i].done)play += "...Done";
  268.  
  269.                 RECT rc = {250, 200 + i*40, 0, 0};
  270.                 m_pCaption->DrawText(NULL, play.c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  271.             }
  272.  
  273.             break;
  274.         }
  275.         case 999:    //Error room
  276.         {
  277.             RECT rc = {0, 300, 800, 330};
  278.             m_pCaption->DrawText(NULL, m_error.c_str(), -1, &rc, DT_CENTER | DT_TOP | DT_NOCLIP, 0xffffffff);
  279.  
  280.             RECT r = {300, 330, 500, 360}; 
  281.             if(TextButton("OK", r, true, mouse))
  282.             {
  283.                 room = 0;
  284.                 m_nameTB.text.clear();
  285.                 m_sessionTB.text.clear();
  286.                 m_sendTB.text.clear();
  287.                 network.Release();
  288.             }
  289.  
  290.             break;
  291.         }
  292.     }
  293. }
  294.  
  295. void LOBBY::DrawTextbox(TEXTBOX &tb)    //Draws a textbox
  296. {
  297.     Square(tb.rect, 0xffffffff);
  298.  
  299.     RECT rc = {tb.rect.left + 10, tb.rect.top + 6, 0, 0};
  300.     m_pSmallText->DrawText(NULL, tb.GetText().c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  301. }
  302.  
  303. bool LOBBY::TextButton(char text[], RECT r, bool border, MOUSE &mouse)    //A text button...
  304. {
  305.     DWORD col = 0xffffffff;
  306.     if(mouse.Over(r))col = 0xffff0000;
  307.  
  308.     if(border)
  309.     {
  310.         Square(r, col);
  311.         m_pCaption->DrawText(NULL, text, -1, &r, DT_CENTER | DT_VCENTER | DT_NOCLIP, col);
  312.     }
  313.     else 
  314.     {
  315.         RECT rc = {r.left + 10, r.top + 4, 0, 0};
  316.         m_pCaption->DrawText(NULL, text, -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, col);
  317.     }
  318.     
  319.     if(mouse.PressInRect(r))
  320.     {
  321.         mouse.DisableInput(300);
  322.         return true;
  323.     }
  324.     else return false;
  325. }
  326.  
  327. void LOBBY::Square(RECT r, DWORD col)        //Draws a simple square
  328. {
  329.     D3DXMATRIX sca;
  330.     D3DXVECTOR2 scale = D3DXVECTOR2(r.right - r.left, r.bottom - r.top);
  331.     D3DXMatrixScaling(&sca, scale.x, scale.y, 1.0f);
  332.     m_pSprite->SetTransform(&sca);
  333.     RECT src = {1,1,2,2};
  334.     m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
  335.     m_pSprite->Draw(m_pUI, &src, NULL, &D3DXVECTOR3(r.left / scale.x, r.top / scale.y, 0.0f), 0xffffffff);
  336.     m_pSprite->End();
  337.  
  338.     D3DXMatrixIdentity(&sca);
  339.     m_pSprite->SetTransform(&sca);
  340.  
  341.     D3DXVECTOR2 rect[] = {D3DXVECTOR2(r.left, r.top), D3DXVECTOR2(r.right, r.top), 
  342.                           D3DXVECTOR2(r.right, r.bottom), D3DXVECTOR2(r.left, r.bottom), 
  343.                           D3DXVECTOR2(r.left, r.top)};    
  344.  
  345.     m_pLine->SetWidth(2.5f);
  346.     m_pLine->Begin();    
  347.     m_pLine->Draw(rect, 5, col);
  348.     m_pLine->End();
  349. }